package com.example.ioc.context;

import com.example.common.utils.Statistics;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

/**
 * {@link org.springframework.boot.SpringApplication#addListeners(ApplicationListener[])}
 *
 * 或者
 * {@link org.springframework.context.ConfigurableApplicationContext#addApplicationListener(ApplicationListener)}
 * {@link org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory(ConfigurableListableBeanFactory)}
 */
@Slf4j
//@Component //可以用它注入
public class LApplicationListener implements ApplicationListener<ApplicationEvent> {

    private static volatile LApplicationListener instance;

    private LApplicationListener() {
        Statistics.logInfo(log);
    }

    public static LApplicationListener getInstance() {
        if (instance == null) {
            synchronized (LApplicationListener.class) {
                if (instance == null) {
                    instance = new LApplicationListener();
                }
            }
        }
        return instance;
    }

    @Override
    public void onApplicationEvent(@NonNull ApplicationEvent event) {
        Statistics.logInfo(log, "onApplicationEvent", event);
    }
}
